#!/bin/sh

## Slackware build script for madwifi-driver

## Written by hollywoodb (hollywoodb@fastmail.fm)
## Maintained by Robby Workman <rworkman@slackbuilds.org>
## Modified by Larry Hajali <larryhaja[at]gmail[dot]com>

KERNELVERSION=${KERNELVERSION:-$(uname -r)}
KERNELPATH=${KERNELPATH:-/lib/modules/${KERNELVERSION}/build}

PRGNAM=madwifi
VERSION=0.9.4
ARCH=${ARCH:-i486}
BUILD=${BUILD:-1}
TAG=${TAG:-_SBo}

PKG_NAME=madwifi-driver
PKG_VERSION=$(echo ${VERSION}_$KERNELVERSION | tr '-' '_')

CWD=$(pwd)
TMP=${TMP:-/tmp/SBo}
PKG=${PKG:-$TMP/package-$PKG_NAME}
OUTPUT=${OUTPUT:-/tmp}

set -e

rm -rf $PKG
mkdir -p $TMP $PKG $OUTPUT
cd $TMP
rm -rf $PRGNAM-$VERSION
tar xvf $CWD/$PRGNAM-$VERSION.tar.gz
cd $PRGNAM-$VERSION
chown -R root:root .
chmod -R u+w,go+r-w,a-s .

# Patches to build madwifi against Slacware 12.2 kernel
# Patch from: http://madwifi-project.org/changeset/3696
patch -p1 < $CWD/patches/madwifi-0.9.4_kernel_2.6.26.diff
# Patch from: http://slackware.com/~alien/slackbuilds/madwifi/build/
patch -p1 < $CWD/patches/madwifi-0.9.4_kernel_2.6.27.diff

KERNELPATH=$KERNELPATH KERNELRELEASE=$KERNELVERSION \
  make modules
KERNELPATH=$KERNELPATH KERNELRELEASE=$KERNELVERSION \
  make install-modules DESTDIR=$PKG

mkdir -p $PKG/usr/doc/$PKG_NAME-$PKG_VERSION
cp -a COPYRIGHT $PKG/usr/doc/$PKG_NAME-$PKG_VERSION
cp -a hal/COPYRIGHT $PKG/usr/doc/$PKG_NAME-$PKG_VERSION/COPYRIGHT.hal
cp -a hal/README $PKG/usr/doc/$PKG_NAME-$PKG_VERSION/README.hal
cat $CWD/$PKG_NAME.SlackBuild > $PKG/usr/doc/$PKG_NAME-$PKG_VERSION/$PKG_NAME.SlackBuild

mkdir -p $PKG/install
cat $CWD/slack-desc > $PKG/install/slack-desc
sed s/_KERNEL_/$KERNELVERSION/ $CWD/doinst.sh > $PKG/install/doinst.sh

cd $PKG
/sbin/makepkg -l y -c n $OUTPUT/$PKG_NAME-$PKG_VERSION-$ARCH-$BUILD$TAG.tgz
